<template>
  <div class="goods-comment">
    <!-- 评价头部 -->
    <div class="head">
      <div class="data">
        <p><span>{{commentInfo.salesCount}}</span><span>人购买</span></p>
        <p><span>{{commentInfo.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            :class="{active:currentTagIndex ===i}"
            v-for="(item,i) in commentInfo.tags"
            :key="item.title" href="javascript:;"
            @click="changeTag(i)"
            >{{item.title}} ({{item.tagCount}}) </a>
        </div>
      </div>
    </div>
    <!-- 评价列表 -->
    <div class="sort" v-if="commentInfo">
      <span>排序：</span>
      <a @click="changeSort(null)" href="javasript:;" :class="{active:reqParams.sortField===null}">默认</a>
      <a @click="changeSort('createTime')" href="javasript:;" :class="{active:reqParams.sortField==='createTime'}">最新</a>
      <a @click="changeSort('praiseCount')" href="javasript:;" :class="{active:reqParams.sortField==='praiseCount'}">最热</a>
    </div>
    <!-- 列表 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{formatNickname(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in item.score" :key="i"></i>
            <i class="iconfont icon-wjx02" v-for="i in (5-item.score)" :key="i"></i>
            <span class="attr" v-for="spec in item.orderInfo.specs" :key="spec.name">{{spec.name}}：&nbsp;{{spec.nameValue}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <!-- 评论图片组件 -->
          <GoodsCommentPicture :pictureList="item.pictures" v-if="item.pictures.length"></GoodsCommentPicture>
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i> ({{item.praiseCount}})</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <XtxPagination v-if="total" @current-page="changePagerFn" :total="total" :page-size="reqParams.pageSize" :current-page="reqParams.page"></XtxPagination>
  </div>
</template>

<script>
import { reactive, ref } from '@vue/reactivity'
import { inject, watch } from '@vue/runtime-core'
import { findCommentInfoByGoods, findGoodsCommentList } from '@/api/product.js'
import GoodsCommentPicture from './goods-comment-picture.vue'
import XtxPagination from '../../../components/library/xtx-pagination.vue'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentPicture, XtxPagination },
  setup () {
    // 获取评价信息
    const goods = inject('goods')
    const commentInfo = ref({})
    findCommentInfoByGoods(goods.value.id).then(data => {
      // 设置数据之前 应该我那往tags数组前追加 有图tag 全部评价tag
      data.result.tags.unshift({ title: '有图', tagCount: data.result.hasPictureCount, type: 'img' })
      data.result.tags.unshift({ title: '全部评价', tagCount: data.result.evaluateCount, type: 'all' })
      commentInfo.value = data.result
      // console.log(commentInfo.value)
    })

    // 激活tag 设置选中样式
    const currentTagIndex = ref(0)
    const changeTag = (index) => {
      currentTagIndex.value = index
      // 点击tag时候 修改筛选条件
      // 情况1：全部评价
      // 情况2：有图
      // 情况3：正常tag
      const tag = commentInfo.value.tags[index]
      if (tag.type === 'all') {
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (tag.type === 'img') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = null
        reqParams.tag = tag.title
      }
      // 页码重置到1
      reqParams.page = 1
      // console.log(reqParams)
    }

    // 准备筛选条件数据
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      // 排序方式 praiseCount 热度 createTime 最新
      sortField: null
    })

    // 初始化需要发请求 筛选条件发生改变时也要重新发送请求
    const commentList = ref([])
    const total = ref(0)
    watch(reqParams, () => {
      findGoodsCommentList(goods.value.id, reqParams).then(data => {
        commentList.value = data.result.items
        // console.log(commentList.value)
        total.value = data.result.counts
      })
    }, { immediate: true })

    const changeSort = (sortField) => {
      // 将页码置为1
      reqParams.page = 1
      reqParams.sortField = sortField
    }

    // 定义转换数据的函数（相当于vue2.0的过滤器）
    const formatNickname = (nickName) => {
      return nickName.substr(0, 1) + '****' + nickName.substr(-1)
    }

    // 实现分页切换，得到子组件传来的新页码，重新发送请求
    const changePagerFn = (newPage) => {
      reqParams.page = newPage
    }

    return { formatNickname, commentInfo, currentTagIndex, changeTag, reqParams, commentList, changeSort, total, changePagerFn }
  }
}
</script>

<style lang="less" scoped>
.goods-comment{
  .head{
    display: flex;
    padding:30px 0;
    .data{
      width: 340px;
      display: flex;
      padding: 20px;
      p{
        flex: 1;
        text-align: center;
        span{
          display: block;
          &:first-child{
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child{
            color: #999;
          }
        }
      }
    }
    .tags{
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt{
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd{
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        >a{
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          background-color: #f5f5f5;
          border:1px solid #a09f9f;
          text-align: center;
          line-height: 40px;
          color:#999;
          &:hover{
            border-color: @xtxColor;
            background-color: lighten(@xtxColor,50%);
            color:@xtxColor;
          }
          &.active{
            background-color: @xtxColor;
            color:#fff;
            border-color: @xtxColor;
          }
        }
      }
    }
  }
  .sort{
    height: 60px;
    line-height: 60px;
    margin: 0 20px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    color:#666;
    >span{
      margin-left: 20px;
    }
    >a{
      margin-left: 30px;
      &.active,&:hover{
        color:@xtxColor;
      }
    }
  }
  .list{
    padding: 0 20px;
    .item{
      display:flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user{
        width: 140px;
        img{
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span{
          padding-left: 10px;
          color: #666;
        }
      }
      .body{
        flex: 1;
        .score{
          line-height: 40px;
          .iconfont{
            color:#ff9240;
            padding-left: 3px;
          }
          .attr{
            padding-left: 10px;
            color:#666;
          }
        }
      }
      .text{
        color: #666;
        line-height: 24px;
      }
      .time{
        color:#999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
